package com.sheng.sign.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.sheng.sign.entity.Classes;

public class ClassesDaoImpl implements ClassesDao {
	
		private SessionFactory sessionFactory;
	 
		public SessionFactory getSessionFactory() {
			return sessionFactory;
		}
		public void setSessionFactory(SessionFactory sessionFactory) {
			this.sessionFactory = sessionFactory;
		}

	@Override
	public boolean add(Classes classes) {
		try {
			classes.setIsDelete(0);
			sessionFactory.getCurrentSession().save(classes);
			return true;
		} catch (HibernateException e) {
			return false;
		}
	}

	@Override
	public boolean edit(Classes classes) {
		try {
			classes.setIsDelete(0);
			sessionFactory.getCurrentSession().update(classes);
			return true;
		} catch (HibernateException e) {
			return false;
		}
	}

	@Override
	public boolean delete(Classes classes) {
		try {
			classes.setIsDelete(1);
			sessionFactory.getCurrentSession().update(classes);
			return true;
		} catch (HibernateException e) {
			return false;
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Classes> findAllList() {
		String hql = "from Classes where 1=1 and is_delete=0";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);  
        List list = query.list();  
        return list;
	}
	public Classes findById(Classes classes) {
		Query query = sessionFactory.getCurrentSession().createQuery("from Classes where id='"+classes.getId()+"'");  
		List list = query.list();  
		if (null != list && 0 < list.size()) {
			classes = (Classes) list.get(0);
		}
		return classes;
	}
	@Override
	public List<Classes> findByClassesName(Classes classes) {
		String hql = "from Classes where  classes_name='"+classes.getClassesName()+"' and is_delete=0 ";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);  
        List list = query.list();  
        return list;
	}

}
